package cn.itcast.controller;

import cn.itcast.Annotation.Cache;
import cn.itcast.entity.EchartsResult;
import cn.itcast.entity.Result;
import cn.itcast.service.ReportService;
import cn.itcast.vo.IncomeStatisticalVo;
import cn.itcast.vo.OrderStatisticalVo;
import cn.itcast.vo.ReportDataVo;
import cn.itcast.vo.SetmealVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private ReportService reportService;

    private ReportDataVo reportDataVo = null;

    /**
     * 运营数据
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/business")
    public Result business() throws Exception {
        Result result = reportService.business();
        //给reportDataVo复赋值,方便后续报表导出
        reportDataVo = (ReportDataVo) result.getData();
        return result;
    }

    /**
     * 导出报表
     *
     * @param response
     * @return
     * @throws Exception
     */
    @GetMapping("/export/business")
    public Result export(HttpServletResponse response, HttpServletRequest request) throws Exception {
        // 用类加载器获取模板文件的路径
        URL url = ReportController.class.getClassLoader().getResource("static/template/report_template.xlsx");
        // 这里是模板的文件路径
        String path = url.getPath();//这里得到的路径前面带一个 / 需要去掉
        String templateFileName = path.substring(1);

        // 这里是本地生成一个临时文件
        String filePathPerfix = path.substring(0, path.lastIndexOf("/"));//这里得到项目的临时文件
        String firstFileName = filePathPerfix + System.currentTimeMillis() + ".xlsx";
        File file = new File(firstFileName);

        ExcelWriter excelWriter = EasyExcel.write(firstFileName).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(reportDataVo, writeSheet);
        excelWriter.fill(reportDataVo.getHotSetmeal(), writeSheet);
        // 千万别忘记关闭流
        excelWriter.finish();

        response.addHeader("Content-Disposition", "attachment;filename="
                + "report" + UUID.randomUUID().toString().replaceAll("-", "") + ".xlsx");
        FileInputStream fis = new FileInputStream(firstFileName);
        ServletOutputStream os = response.getOutputStream();
        int count = 0;
        byte[] by = new byte[1024];
        while ((count = fis.read(by)) != -1) {
            os.write(by, 0, count);//将缓冲区的数据输出到浏览器
        }
        fis.close();
        os.close();
//        删除临时文件
        if (file.exists()) {
            file.delete();
        }
        return new Result(true, "导出成功！");
    }


    //会员数量
    @Cache
    @GetMapping("/member/count")
    public Result MemberCount() {

        Result result = reportService.queryMemberAndMothods();

        return result;
    }

    //套餐饼状图
    @Cache
    @GetMapping("/setmeals")
    public Result setmeals(){
        Result result = reportService.setmeals();

        return result;
    }

    /**
     * 查询收入统计
     *
     * @return
     */
    @GetMapping("/income")
    public Result income() {
        IncomeStatisticalVo vo = reportService.incomeStatistical();
        return new Result(true, "查询成功！", vo);
    }

    /**
     * 查询预约支付统计
     *
     * @return
     */
    @GetMapping("/order")
    public Result order() {
        OrderStatisticalVo vo = reportService.orderStatistical();
        return new Result(true, "查询成功！", vo);
    }



}